package com.dingding.service.activity.core.dao.impl;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.ActivityRelevance;
import com.dingding.common.core.util.RandomNumber;
import com.dingding.service.activity.core.dao.IActivityShareDao;
import com.dingding.service.activity.core.sql.ActivityShareSQL;

/**
 * 
 * @ClassName: UserActivityDaoImpl
 * @Description: 用户活动信息
 * @author cw
 * @date 2016年5月25日15:45:26
 * 
 */
@Component("activitySharDao")
public class ActivityShareDaoImpl extends BaseDaoImpl<ActivityRelevance> implements IActivityShareDao {

	private static final Logger log = Logger.getLogger(ActivityShareDaoImpl.class);

	/**
	 * 
	 * @Title: isExistUserByUserName
	 * @Description: 判断用户邀请码是否存在
	 * @param invitationCode
	 *            用户邀请码
	 * @return int 1: 邀请码 可以使用 2: 邀请码 已经使用 3:邀请码 不能为空
	 * @author cw
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public int isExistInvitationCode(String invitationCode) {

		ActivityRelevance userActivity = getUserActivityByProperty("invitationCode", invitationCode);

		if (userActivity != null) {
			int isAttendActivity = userActivity.getIsAttendActivity();

			if (isAttendActivity == 1) { // 邀请码存在，但是未参加活动
				return 1;
			} else { // 邀请码存在，但是已经参加活动
				return 2;
			}
		} else {
			return 3; // 邀请码不存在
		}
	}

	/**
	 * 
	 * @Title: getUserLoginByProperty
	 * @Description: 根据某一属性查询用户登录表相关信息
	 * @param property
	 *            根据某一属性查询用户
	 * @param value
	 *            属性值
	 * @return UserLogin 查询到的用户登录信息
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	public ActivityRelevance getUserActivityByProperty(String property, String value) {

		Criterion criterion = Restrictions.eq(property, value);
		ActivityRelevance userActivity = null;
		try {
			userActivity = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("根据数据获取用户登录信息出现异常！", e);
		}
		return userActivity;
	}

	/**
	 * 
	 * @Title: getUserActiveInfo
	 * @Description: 获取用户活动信息
	 * @param userId
	 *            用户id activityId 活动id
	 * @return 用户活动信息
	 * @author cw
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public ActivityRelevance getUserActiveInfo(String userId, String activityId) {
		ActivityRelevance userActivity = null;
		// 创建查询条件
		Criterion criterion = Restrictions.and(Restrictions.eq("userId", userId), // 2：app主页显示
				Restrictions.eq("activityId", activityId));
		// Restrictions.eq("invitationCode", invitationCode));
		try {
			userActivity = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("获取用户活动信息失败！UserActivityDaoImpl getUserActiveInfo", e);
		}

		return userActivity;
	}

	/**
	 * 
	 * @Title: addUserActivity
	 * @Description: 添加用户活动信息
	 * @param userActivity
	 *            用户活动
	 * @author cw
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUserActivity(String userId, String activityId) {

		ActivityRelevance userActivity = new ActivityRelevance();
		userActivity.setUserId(userId);
		userActivity.setActivityId(activityId);

		String invitationCode = "1" + RandomNumber.generateRandomNumber(6).toString();
		// 生成活动邀请码
		userActivity.setInvitationCode(invitationCode);
		
		userActivity.setActivityNumber(0);
		userActivity.setIsAttendActivity(1);

		try {
			// 向用户账户表插入一条记录
			super.save(userActivity);

			return true;
		} catch (Exception e) {
			log.error("添加用户活动关联信息出现异常！", e);
		}

		return false;
	}

	/**
	 * 
	 * 修改用户统计次数
	 * 
	 * @Title: addUserActivity
	 * @param userActivity
	 *            用户活动
	 * @author cw
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean setUserActivityNumber(String userId, String activityId) {
		int excuteRow = 0;
		try {
			
			ActivityRelevance activityRelevance = getUserActiveInfo(userId, activityId);
			
			if (activityRelevance != null)
				activityRelevance.setActivityNumber(activityRelevance.getActivityNumber() + 1);
			
			save(activityRelevance);
			
			// excuteRow = super.updateSqlRow(ActivityShareSQL.UPDATE_ACTIVITY_RELEVANCE_NUMBER, userId, activityId);
		} catch (Exception e) {
			log.error("统计用户分享信息出错！", e);
		}
		if (excuteRow != 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @Title: setActivityRelevanceState
	 * @Description: 修改用户是否已经参与过活动的状态
	 * @param userId	用户Id
	 *        activityId    活动Id
	 * @author cw
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean setActivityRelevanceState(String userId, String activityId){
		int excuteRow = 0;
		try {
			excuteRow = super.updateSqlRow(ActivityShareSQL.UPDATE_ACTIVITY_RELEVANCE_STATE, userId, activityId);
		} catch (Exception e) {
			log.error("修改用户分享送券活动状态失败！", e);
		}
		if (excuteRow != 0) {
			return true;
		}
		return false;
	}
}
